package com.example.swd1.gudong.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;

import java.util.ArrayList;
import java.util.List;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-23
//   Role :    the view show in the bottom at the main page
//   Reference documents : None
//==================================================================================================================
public class TabView extends LinearLayout implements View.OnClickListener {
    private Context mContext;

    private TabItem mSportRangBtn = null;
    private TabItem mFoundBtn = null;
    private TabItem mSportBtn = null;
    private TabItem mMsgBtn = null;
    private TabItem mMeBtn = null;
    private int DEFALUT_BACKGROUND_COLOR = getResources().getColor(R.color.codoon_group_backgroud);
    private BottomPanelCallback mBottomCallback = null;
    private List<TabItem> viewList = new ArrayList<TabItem>();

    public interface BottomPanelCallback {
        void onBottomPanelClick(int itemId);
    }

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onFinishInflate() {
        // TODO Auto-generated method stub
        mSportRangBtn = (TabItem) findViewById(R.id.btn_sport_rang);
        mFoundBtn = (TabItem) findViewById(R.id.btn_found);
        mSportBtn = (TabItem) findViewById(R.id.btn_sport);
        mMsgBtn = (TabItem) findViewById(R.id.btn_message);
        mMeBtn = (TabItem) findViewById(R.id.btn_me);
        setBackgroundColor(DEFALUT_BACKGROUND_COLOR);
        viewList.add(mSportRangBtn);
        viewList.add(mFoundBtn);
        viewList.add(mSportBtn);
        viewList.add(mMsgBtn);
        viewList.add(mMeBtn);

    }

    public void initBottomPanel() {
        if (mSportRangBtn != null) {
            mSportRangBtn.setImage(R.drawable.ic_tab_sport_rang_normal);
            mSportRangBtn.setText("运动圈");
        }
        if (mFoundBtn != null) {
            mFoundBtn.setImage(R.drawable.ic_tab_dis_normal);
            mFoundBtn.setText("发现");
        }
        if (mSportBtn != null) {
            mSportBtn.setImage(R.drawable.ic_tab_sport_normal);
            mSportBtn.setText("运动");
        }
        if (mMsgBtn != null) {
            mMsgBtn.setImage(R.drawable.ic_tab_msg_normal);
            mMsgBtn.setText("消息");
        }
        if (mMeBtn != null) {
            mMeBtn.setImage(R.drawable.ic_tab_me_normal);
            mMeBtn.setText("我的");
        }
        setBtnListener();
    }

    public void setUnReadMessageStatus(boolean isShow,int num){
        if(isShow){
            mMsgBtn.setUnReadMessage(num);
        }else {
            mMsgBtn.hideUnReadMessage();
        }
    }

    public int getUnReadMessageStatus(){
        return Integer.parseInt(mMsgBtn.getUnReadMessage());
    }

    private void setBtnListener() {
        int num = this.getChildCount();
        for (int i = 0; i < num; i++) {
            View v = getChildAt(i);
            if (v != null) {
                v.setOnClickListener(this);
            }
        }
    }

    public void setBottomCallback(BottomPanelCallback bottomCallback) {
        mBottomCallback = bottomCallback;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        initBottomPanel();
        int index = -1;
        switch (v.getId()) {
            case R.id.btn_sport_rang:
                index = Constant.BTN_FLAG_SPORTRANG;
                mSportRangBtn.setChecked(Constant.BTN_FLAG_SPORTRANG);
                break;
            case R.id.btn_found:
                index = Constant.BTN_FLAG_FOUND;
                mFoundBtn.setChecked(Constant.BTN_FLAG_FOUND);
                break;
            case R.id.btn_sport:
                index = Constant.BTN_FLAG_SPORT;
                mSportBtn.setChecked(Constant.BTN_FLAG_SPORT);
                break;
            case R.id.btn_message:
                index = Constant.BTN_FLAG_MESSAGE;
                mMsgBtn.setChecked(Constant.BTN_FLAG_MESSAGE);
                break;
            case R.id.btn_me:
                index = Constant.BTN_FLAG_ME;
                mMeBtn.setChecked(Constant.BTN_FLAG_ME);
                break;
            default:
                break;
        }
        if (mBottomCallback != null) {
            mBottomCallback.onBottomPanelClick(index);
        }
    }

    public void defaultBtnChecked(int flag) {
        if (mSportBtn != null) {
            mSportBtn.setChecked(flag);
        }
    }
}
